package com.yuhuofei2021.spring.boot.weather.service.implement;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import com.yuhuofei2021.spring.boot.weather.entity.City;
import com.yuhuofei2021.spring.boot.weather.entity.CityList;
import com.yuhuofei2021.spring.boot.weather.service.CityDataService;
import com.yuhuofei2021.spring.boot.weather.util.XmlBuilder;

/**
 * @description 获取城市列表接口的实现
 * @author yuhuofei2021
 * @date 2021年9月21日
 */
@Service
public class CityDataServiceImplement implements CityDataService {

	@Override
	public List<City> listCity() throws Exception {
		//读取XML文件，并用字符串接收
		ClassPathResource resource = new ClassPathResource("citylist.xml");
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource.getInputStream(),"UTF-8"));
		StringBuffer buffer = new StringBuffer();
		String lineString = "";
		
		while ((lineString = bufferedReader.readLine()) != null) {
			buffer.append(lineString);
		}
		
		bufferedReader.close();
		
		//将XML元素转换为Java对象
		CityList cityList = (CityList) XmlBuilder.xmlStrToOject(CityList.class, buffer.toString());
		return cityList.getCityList();
	}

}
